package com.hunt.service.app.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hunt.dao.FinancialDataMapper;
import com.hunt.dao.QuotationsFlowMapper;
import com.hunt.model.app.QuotationsFlow;
import com.hunt.model.app.QuotationsFlowBefor;
import com.hunt.service.app.QuotationsFlowService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class QuotationsFlowServiceImpl implements QuotationsFlowService {

    @Autowired
    private QuotationsFlowMapper quotationsFlowMapper;
    @Autowired
    private FinancialDataMapper financialDataMapper;
    @Resource
    private RedisTemplate stringredisTemplate;

    @Override
    public Integer saveOrUpdateQuotationFlow(QuotationsFlow quotationsFlow) {
        return quotationsFlowMapper.saveOrUpdateQuotationFlow(quotationsFlow);
    }

    @Override
    public QuotationsFlow getQuotationFlowByStockCode(String stockCode) {
        Object stockBaseMessage = stringredisTemplate.opsForHash().get("Stock_Base_Message",stockCode);
        JSONObject stockBaseMessagejo = JSON.parseObject((String) stockBaseMessage);
        double floatsharesstock = 0.0;
        try {
            if (stockBaseMessagejo.getFloat("floatShares") != null) {
                floatsharesstock = stockBaseMessagejo.getDouble("floatShares")*10000;
            }
        } catch (Exception e) {

        }
        JSONObject j1=new JSONObject();
       j1.put("a",floatsharesstock);
        Date date=new Date();
        SimpleDateFormat s1=new SimpleDateFormat("yyyyMMdd");
        String dateformate= s1.format(date);
        JSONObject j2=new JSONObject();
        j2.put("date",dateformate);
        j2.put("capital",j1);
        JSONArray ja1=new JSONArray();
        ja1.add(j2);
        JSONObject j3=new JSONObject();
        j3.put("stockday",ja1);
        j3.put("symbol",stockBaseMessagejo.getString("ts_code"));
        j3.put("name",stockBaseMessagejo.getString("name"));
        SimpleDateFormat s2=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateformate1= s2.format(date);
        j3.put("count",1);
        JSONArray ja2=new JSONArray();
        ja2.add(j3);
        JSONObject j4=new JSONObject();
        j4.put("stock",ja2);
        j4.put("count",1);
        j4.put("servertime",dateformate1);
        j4.put("start",0);
        j4.put("end",0);
        j4.put("ticket",0);
        j4.put("version","HQ.StockHistoryDay 2.0");
        j4.put("message",0);
        j4.put("code",0);
        QuotationsFlow quotationsFlow=new QuotationsFlow();
        quotationsFlow.setMessage(JSON.toJSONString(j4));
        return quotationsFlow;
    }
}
